(function($) {
	$(':text[rel~=uniqueusername]').live("blur", function(e) {
		e.preventDefault();
		var field = $(this);
		var form_container = field.parents('form');
		if(field.val() == ''){
			return false;
		}
		
		form_container.find('input[type="submit"], input[type="button"], button').prop('disabled', true);
		field.prop('disabled', true);
		$.getJSON($(this).data('url'), {'username': field.val()})
		.always(function() {
			form_container.find('input[type="submit"], input[type="button"], button').prop('disabled', false);
			field.prop('disabled', false);
  		})
		.done(function(data) {
		    if(data.result != 'success'){
		    	alert(data.msg);
		    	field.val('');
		    	field.focus();
		    }
		})
		.fail(function( jqxhr, textStatus, error ) {
			alert('Error occur while loading, please try again.');
		    field.val('');
		    field.focus();
		});
	});
})(jQuery);
